#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <cmath>
using namespace std;
/* #ifdef ONLINE_JUDGE */

/* #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++) */
/* char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf; */

/* #endif */
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
const int N = 2005;
char strbuf[N];
string mp[N], s;
array <int, N> isl;
double ans_;
string ansS;
void dfs(int step, int n, int m, string tp) {
	/* printf("%s\n", tp.c_str()); */
	if (step >= n) {
		int ans = 1, t = 0;
		for (int i = 0; i < n; i++) {
			for (int j = 1; j <= m; j++) {
				if (mp[j].size() + i >= n) continue;
				bool flg = false;
				for (int k = 0; k < mp[j].size(); k++) {
					if (tp[i + k] == mp[j][k])
					   	continue;
					flg = 1;
					break;
				}
				if (!flg) ans *= isl[j], t++;
			}
		}
		/* if (tp == "11012") cout << pow(1.0 * ans, 1.0 / t), puts("@@"); */
		if (pow(1.0 * ans, 1.0 / t) > ans_)
			ans_ = pow(1.0 * ans, 1.0 / t), ansS = tp;
		return;
	}	
	while (step <= n && s[step] != '.') tp.push_back(s[step++]);
	for (int i = '0'; i <= '9'; i++) {
		string tp2 = tp;
		tp2 += i;
		dfs(step + 1, n, m, tp2);
	}
}
void Sub1(int n, int m) {
	dfs(0, n, m, "");
	printf("%s\n", ansS.c_str());
}
void Sub2(int n, int m) {
	for (int i = 0; i < n; i++) {
		bool flg_ = false;
		for (int j = 1; j <= m; j++) {
			if (mp[j].size() + i >= n) continue;
			bool flg = false;
			for (int k = 0; k < mp[j].size(); k++) {
				if (s[i + k] == '.' || s[i + k] == mp[j][k])
				   	continue;
				flg = 1;
				break;
			}
			if (flg) continue;
			for (int k = 0; k < mp[j].size(); k++)
				s[i + k] = mp[j][k];
			for (int k = 0; k < n; k++) {
				if (s[k] != '.') continue;
				s[k] = '0';
			}
			flg_ = 1;
			break;
		}
		if (flg_) break;
	}
	printf("%s\n", s.c_str());
}
int main() {
	freopen("magic.in", "r", stdin);
	freopen("magic.out", "w", stdout);
	int n = read(), m = read();
	scanf("%s", strbuf);
	s = strbuf;
	bool flg = true;
	for (int i = 1; i <= m; i++) {
		scanf("%s", strbuf);
		mp[i] = strbuf;
		isl[i] = read();
		if (isl[i] != isl[1]) flg = false;
	}
	if (flg) Sub2(n, m);
	else if (n <= 6) Sub1(n, m);
	return 0;
}
